/**
 * 蓓兰国际公众号二维码
 */
'use strict';
import React,{Component} from 'react';
import Header from '../common/header';
import styles from '../style/styles';
import Icon from 'react-native-vector-icons';
import * as URLconstant from '../constant/URLconstant';
import Util from '../common/util'
import Toast from '../common/ToastUtil';
import {
    View,
    Text,
    TouchableOpacity,
    Image,
    Alert,
    NativeModules,
    CameraRoll,
    Platform
} from 'react-native';
const SaveModule =NativeModules.SavePicture;
export default class blgjcode extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.successCallback = this.successCallback.bind(this);
        this.errorcallback = this.errorcallback.bind(this);
        this.DetermineShow=this.DetermineShow.bind(this);
        this.state = {
            loaded: true,
            codeImg:''
        };
    }

    componentDidMount() {
        let that = this;
        let datakey = [];
        datakey['signme']='1'
        let abouturl = URLconstant.QRCODE_URL;
        Util.get(abouturl, datakey, this.successCallback, this.errorcallback);
    }

    successCallback(datas) {
        LOG('成功===' + JSON.stringify(datas));
        if(datas.status==0){
            let data = datas.data;
            this.setState({
                codeImg:data,
                loaded:false
            })
        }else {
            Toast.show(datas.msg)
            this.props.navigator.pop();
        }


    }

    errorcallback() {
        LOG('请求失败')
    }

    onSaveImage(ImgUrl) {
        let alertMessage = '保存图片'
        Alert.alert(
            '提示',
            alertMessage,
            [
                {text: '取消', onPress: () => this.cancelShow()},
                {text: '确定', onPress: () => this.DetermineShow(ImgUrl)},
            ]
        )
    }
    cancelShow(){
        Toast.show("取消图片保存", 3);
    }


    //CameralRoll在IOS平台中需要添加链接库才能运行，否则报错找不到api：
    //1.\node_modules\React-native\Libraries\CameraRoll下的Xcode项目文件RCTCameraRoll.xcodeproj拖动到当前Xcode项目的Libraries目录
    //2.选中当前项目，在右边选择Build Phases，点击打开子项目Link Binary With Libraris
    //3.打开第一步插入的RCTCameraRoll.xcodeproj，再打开它的子目录Products，将子目录下的libRCTCameraRoll.a文件拖到Link Binary With Libraris列表中

    DetermineShow(ImgUrl){
        LOG('ImgUrl=='+ImgUrl)
        if(Platform.OS === 'android'){
            SaveModule.initSave(ImgUrl);
        }else {
            CameraRoll.saveImageWithTag(ImgUrl).done(function (data) {
                LOG("保存图片成功 data==" + data);
                Toast.show("保存图片成功", 2);
            }, function (err) {

                LOG("保存图片失败 err==" + err);
                Toast.show("保存图片失败", 2);

            });
        }


    }


    render() {
        let codeImg = this.state.codeImg
        //LOG('成功===' + codeImg);
        return (
            <View style={{flex:1,}}>
                <Header navigator={this.props.navigator} initObj={{ title:'关注我们',id:2}}/>
                {this.state.loaded ?
                    <View style={{flex:1}}>
                        {Util.loading()}</View>
                    :
                    <View style={{flex: 1,justifyContent:'center',alignItems:'center'}}>
                        <TouchableOpacity onLongPress={()=>this.onSaveImage(codeImg.toString())}>
                            <Image
                                resizeMode="stretch"
                                style={{width:Util.size.width-20,height:Util.size.width-20}}
                                source={{uri:codeImg}}/>
                        </TouchableOpacity>
                    </View>
                }

            </View>
        )
    }
}